/**
 * Created by zlq on 2017/9/30.
 */


var page = 1;
var id;
var realName,
    userName,
    address,
    jiangPinName,
    logisticsCode;

function doAjax(){
    $.ajax({
        url:'/crm/zongJiangRecordC/getZongJiangRecord',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            page:page,
            obtainStateValue:$('#obtainStateValue').val(),
            jiangPinLevelValue:$('#jiangPinLevelValue').val(),
            phone:$('#phone').val(),
            code:$('#code').val(),
            jiangPinCode:$('#jiangPinCode').val(),
        },
        type:'post',
        success: function (data) {
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                $('#record').html(data.recordsTotal);
                $('#withdraw-record').empty();
                for(var i=0;i<data.datas.length;i++){
                    var obtainStateValue=data.datas[i].obtainStateValue;
                    var deliverGoods="";
                    if(obtainStateValue==2){
                        deliverGoods="发货";
                    }

                    $('<tr class="gradeA odd">'+
                        '<a href=""><td class="detailsId"   data-toggle="modal" data-target="#details"  style="text-decoration: underline;cursor: pointer;" realName="'+data.datas[i].realName+'"  userName="'+data.datas[i].userName+'" address="'+data.datas[i].address+'" jiangPinName="'+data.datas[i].jiangPinName+'" logisticsCode="'+data.datas[i].logisticsCode+'">'+data.datas[i].id+'</td></a>'+
                        '<td class="sorting_1">'+data.datas[i].userName+'</td>'+
                        '<td class="center ">'+data.datas[i].code+'</td>'+
                        '<td class="center ">'+data.datas[i].jiangPinLevel +'</td>'+
                        '<td class="center ">'+data.datas[i].createTime+'</td>'+
                        '<td class="center ">'+data.datas[i].jiangPinName+'</td>'+
                        '<td class=" center">'+data.datas[i].obtainState+'</td>'+
                        '<td class=" center">'+data.datas[i].obtainTime+'</td>'+
                        '<td class=" center">'+data.datas[i].logisticsCode+'</td>'+
                        '<td class="centerDeliverGoods"  data-toggle="modal" data-target="#deliverGoods" style="text-decoration: underline;cursor: pointer;" id="'+data.datas[i].id+'" realName="'+data.datas[i].realName+'"  userName="'+data.datas[i].userName+'" address="'+data.datas[i].address+'" jiangPinName="'+data.datas[i].jiangPinName+'">'+deliverGoods+'</td>'+
                        '</tr>').appendTo($('#withdraw-record'));
                }

                $(".pagination").createPage({
                    totalPage:data.totalPage,
                    currPage:data.pageNow,
                    pageCount:data.pageNow,
                    turndown:'true',
                    backFn:function(p){
                        page=p;
                        doAjax();
                    }
                });

                $('.detailsId').click(function(){
                    id = $(this).attr('id');
                    realName= $(this).attr('realName');
                    userName= $(this).attr('userName');
                    address= $(this).attr('address');
                    jiangPinName= $(this).attr('jiangPinName');
                    logisticsCode= $(this).attr('logisticsCode');
                    $('#jianPinName1').val(jiangPinName);
                    $('#realName1').val(realName);
                    $('#phone1').val(userName);
                    $('#location1').val(address);
                    $('#logisticsCode1').val(logisticsCode);

                })

                $('.centerDeliverGoods').click(function(){
                    id = $(this).attr('id');
                    realName= $(this).attr('realName');
                    userName= $(this).attr('userName');
                    address= $(this).attr('address');
                    jiangPinName= $(this).attr('jiangPinName');
                    $('#jianPinName2').val(jiangPinName);
                    $('#realName2').val(realName);
                    $('#phone2').val(userName);
                    $('#location2').val(address);

                })

            }
        }
    })
}

$(function(){
    doAjax();
})


//点击保存
$('#save').click(function(){

    $.ajax({
        url:'/crm/zongJiangRecordC/jiangPinShipments',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            id:id,
            name:$('#name').val(),
            logisticsCode:$('#logisticsCode2').val(),
        },
        type:'post',
        success:function(data){
            if(data.code==0){
                alert(data.msg)
                return;
            }
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==1){
                alert('发货成功');
                $('#deliverGoods').removeClass('fade in');
                $('#deliverGoods').addClass('fade');
                $('#deliverGoods').attr('aria-hidden',true);
                $('#deliverGoods').css('display','none');
                $('.modal-backdrop').css('display','none');
                $('#deliverGoods input').val('');
                page=1;
                doAjax();
            }
        }
    })
})

//查询
$('#refer').click(function(){
    stateValue = $('#state').val();
    page=1;
    doAjax();
})
